package com.herench.bs.handler;


import com.herench.bs.transfer.Dialect;
import com.herench.bs.agent.IPersonAgent;
import com.herench.bs.annotation.Handler;
import com.herench.bs.support.AbstractMessageHandler;
import com.herench.bs.support.RequestMessage;
import com.herench.bs.support.ResponseMessage;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;


/**
 * Created by ThinkPad on 2017/8/27.
 */
@Component
@Handler(code = "100203",name="个人基本信息查询")
public class PersonInfoHandler extends AbstractMessageHandler {

    @Override
    protected ResponseMessage innerHandler(RequestMessage requestMessage) throws Exception {
        ResponseMessage responseMessage = null;
        responseMessage = new ResponseMessage(requestMessage.getId());
        String user = requestMessage.getArgsPropertyStringValue("user");
        Assert.hasLength(user, "user 不能为空！");
        String idCard = requestMessage.getArgsPropertyStringValue("idCard");
        String channelCode = requestMessage.getArgsPropertyStringValue("sysCode");
        IPersonAgent personAgent = getCleverClinicContext().getApplicationContext().getBean(IPersonAgent.class);
        Map<String, Object> res = personAgent.loadInfo(idCard, channelCode, user);
        if (Dialect.getSuccessfulState().equals(res.get("code"))) {
            responseMessage.setData(res);
            //不需要绑定设备
            responseMessage.setDataAttribute("ifBindDevice", "1");
            List<Map<String, Object>> cardList = (List<Map<String, Object>>) res.get("cards");
            if (cardList != null && !cardList.isEmpty()) {
                for (Map<String, Object> card : cardList) {
                    card.remove("hospitalCode");
                    card.remove("personId");
                    card.remove("createTime");
                }
            }
            responseMessage.setDataAttribute("cardList", cardList);
        }


        getPropertiesFromInvokerResultMap(responseMessage, res);
        return responseMessage;
    }
}
